/**
 * @file loader.js.
 * @author liaoxuezhi@baidu.com
 */
/* eslint-disable */

import './form/third-form-item';
import './third-factory';

const mapping = {
  'react': __moduleId('react'),
  'react-dom': __moduleId('react-dom'),
  'react-addons-update': __moduleId('react-addons-update'),
  'immutability-helper': __moduleId('react-addons-update'),
  'react-redux': __moduleId('react-redux'),
  'react-router': __moduleId('react-router'),
  'redux': __moduleId('redux'),
  'react-router-redux': __moduleId('react-router-redux'),
  'react-select': __moduleId('react-select'),
  'amis': __moduleId('./index.jsx'),
  'amis/renderer': __moduleId('./third-factory.jsx'),
  'amis/form-item': __moduleId('./form/third-form-item'),
  'amis/util':  __moduleId('./util.jsx'),
  'amis/embed': __moduleId('../page/embed.jsx'),
  'amis/storage': __moduleId('../util/storage'),
  'lodash': __moduleId('lodash'),
  'react-bootstrap': __moduleId('react-bootstrap'),
  'react-input-range': __moduleId('react-input-range'),
  'object-assign': __moduleId('object-assign'),
  'classnames': __moduleId('classnames'),
  'react-mixin': __moduleId('react-mixin'),
  'axios': __moduleId('axios'),
  'react-onclickoutside': __moduleId('react-onclickoutside'),
  'moment': __moduleId('moment'),
  'react-monaco': __moduleId('../widgets/editor/monaco'),
  'visibility-sensor': __moduleId('../widgets/visibility-sensor.jsx')
};

window.amisRequire = function (...args) {
  let id = args.shift();
  id = Array.isArray(id) ? id.map(id => mapping[id] || id) : mapping[id] || id;
  args.unshift(id);
  return require.apply(this, args);
};

export function runOtherJs(arrContent) {
  if (!arrContent) {
    return;
  }
  if ('string' === typeof arrContent) {
    arrContent = [arrContent];
  }

  const handledScript = arrContent.map(function(value) {
  	return ';(function(require, exports, module){try{' + value + '}catch(e){console.warn(e)}})(amisRequire, {}, {})';
  });
  
  const strScript = handledScript.join('');

  window.eval(strScript);
}
